"""
主题模块，定义组件库的基本样式和颜色
"""
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtWidgets import QApplication


class Theme:
    """主题类，定义应用的颜色和样式"""
    
    # 颜色
    PRIMARY = "#409EFF"
    PRIMARY_LIGHT = "#a0cfff"
    PRIMARY_LIGHTER = "#ecf5ff"
    PRIMARY_DARK = "#337ecc"
    
    SUCCESS = "#67C23A"
    SUCCESS_LIGHT = "#e1f3d8"
    
    WARNING = "#E6A23C"
    WARNING_LIGHT = "#faecd8"
    
    DANGER = "#F56C6C"
    DANGER_LIGHT = "#fde2e2"
    
    INFO = "#909399"
    INFO_LIGHT = "#e9e9eb"
    
    # 文本颜色
    TEXT_PRIMARY = "#303133"
    TEXT_REGULAR = "#606266"
    TEXT_SECONDARY = "#909399"
    TEXT_PLACEHOLDER = "#C0C4CC"
    
    # 边框颜色
    BORDER_BASE = "#DCDFE6"
    BORDER_LIGHT = "#E4E7ED"
    BORDER_LIGHTER = "#EBEEF5"
    BORDER_EXTRA_LIGHT = "#F2F6FC"
    
    # 背景颜色
    BG_COLOR = "#FFFFFF"
    BG_COLOR_PAGE = "#F2F3F5"
    
    # 字体大小
    FONT_SIZE_EXTRA_LARGE = 20
    FONT_SIZE_LARGE = 18
    FONT_SIZE_MEDIUM = 16
    FONT_SIZE_BASE = 14
    FONT_SIZE_SMALL = 13
    FONT_SIZE_EXTRA_SMALL = 12
    
    # 圆角
    BORDER_RADIUS_BASE = 4
    BORDER_RADIUS_SMALL = 2
    
    # 阴影
    BOX_SHADOW_BASE = "0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04)"
    BOX_SHADOW_LIGHT = "0 2px 12px 0 rgba(0, 0, 0, 0.1)"
    
    # 过渡
    TRANSITION_DURATION = "0.3s"
    
    # 字体
    FONT_FAMILY = "Microsoft YaHei, Arial, sans-serif"
    
    @classmethod
    def apply_theme(cls, app: QApplication):
        """应用主题到整个应用"""
        print(f"正在应用主题到应用: {app}")
        
        if not app:
            print("警告: 传入的app对象为None，无法应用主题")
            return
            
        try:
            palette = QPalette()
            palette.setColor(QPalette.Window, QColor(cls.BG_COLOR))
            palette.setColor(QPalette.WindowText, QColor(cls.TEXT_PRIMARY))
            palette.setColor(QPalette.Base, QColor(cls.BG_COLOR))
            palette.setColor(QPalette.AlternateBase, QColor(cls.BG_COLOR_PAGE))
            palette.setColor(QPalette.ToolTipBase, QColor(cls.BG_COLOR))
            palette.setColor(QPalette.ToolTipText, QColor(cls.TEXT_PRIMARY))
            palette.setColor(QPalette.Text, QColor(cls.TEXT_PRIMARY))
            palette.setColor(QPalette.Button, QColor(cls.BG_COLOR))
            palette.setColor(QPalette.ButtonText, QColor(cls.TEXT_PRIMARY))
            palette.setColor(QPalette.BrightText, QColor(cls.DANGER))
            palette.setColor(QPalette.Link, QColor(cls.PRIMARY))
            palette.setColor(QPalette.Highlight, QColor(cls.PRIMARY))
            palette.setColor(QPalette.HighlightedText, QColor(cls.BG_COLOR))
            
            app.setPalette(palette)
            
            # 设置全局样式表
            style_sheet = f"""
                * {{
                    font-family: "{cls.FONT_FAMILY}";
                    font-size: {cls.FONT_SIZE_BASE}px;
                }}
            """
            app.setStyleSheet(style_sheet)
            print("主题应用成功")
        except Exception as e:
            print(f"应用主题时出错: {e}")
        
    def get_style_sheet(self, component_name=None):
        """获取应用的样式表"""
        if component_name:
            return self._get_component_style_sheet(component_name)
        
        # 返回全局样式表
        return f"""
            /* 全局样式 */
            * {{
                font-family: {Theme.FONT_FAMILY};
                font-size: {Theme.FONT_SIZE_BASE}px;
            }}
            
            /* 按钮样式 */
            QPushButton {{
                padding: 8px 16px;
                border-radius: {Theme.BORDER_RADIUS_BASE}px;
                border: 1px solid {Theme.BORDER_BASE};
                background-color: {Theme.BG_COLOR};
                color: {Theme.TEXT_REGULAR};
                min-height: 32px;
            }}
            
            QPushButton:hover {{
                background-color: {Theme.BG_COLOR_PAGE};
                border-color: {Theme.BORDER_LIGHT};
            }}
            
            QPushButton:pressed {{
                background-color: {Theme.BORDER_LIGHT};
            }}
            
            QPushButton:disabled {{
                background-color: {Theme.BORDER_LIGHTER};
                border-color: {Theme.BORDER_LIGHT};
                color: {Theme.TEXT_PLACEHOLDER};
            }}
            
            /* 主要按钮 */
            QPushButton[type="primary"] {{
                background-color: {Theme.PRIMARY};
                border-color: {Theme.PRIMARY};
                color: white;
            }}
            
            QPushButton[type="primary"]:hover {{
                background-color: {Theme.PRIMARY_LIGHT};
                border-color: {Theme.PRIMARY_LIGHT};
            }}
            
            QPushButton[type="primary"]:pressed {{
                background-color: {Theme.PRIMARY_DARK};
                border-color: {Theme.PRIMARY_DARK};
            }}
            
            QPushButton[type="primary"]:disabled {{
                background-color: {Theme.PRIMARY_LIGHT};
                border-color: {Theme.PRIMARY_LIGHT};
                color: white;
                opacity: 0.7;
            }}
            
            /* 输入框样式 */
            QLineEdit, QTextEdit, QPlainTextEdit, QSpinBox, QDoubleSpinBox, QComboBox {{
                padding: 8px 12px;
                border: 1px solid {Theme.BORDER_BASE};
                border-radius: {Theme.BORDER_RADIUS_BASE}px;
                background-color: {Theme.BG_COLOR};
                selection-background-color: {Theme.PRIMARY_LIGHT};
                min-height: 20px;
            }}
            
            QLineEdit:hover, QTextEdit:hover, QPlainTextEdit:hover, QSpinBox:hover, QDoubleSpinBox:hover, QComboBox:hover {{
                border-color: {Theme.BORDER_LIGHT};
            }}
            
            QLineEdit:focus, QTextEdit:focus, QPlainTextEdit:focus, QSpinBox:focus, QDoubleSpinBox:focus, QComboBox:focus {{
                border-color: {Theme.PRIMARY};
            }}
            
            /* 消息样式 */
            QLabel#message {{
                padding: 10px 16px;
                border-radius: {Theme.BORDER_RADIUS_BASE}px;
                border: 1px solid {Theme.BORDER_LIGHT};
                background-color: {Theme.BG_COLOR};
                color: {Theme.TEXT_REGULAR};
                font-size: {Theme.FONT_SIZE_BASE}px;
                font-weight: bold;
                border-left-width: 4px;
                line-height: 1.5;
            }}
            
            QLabel#message[type="success"] {{
                background-color: {Theme.SUCCESS_LIGHT};
                border-color: {Theme.SUCCESS_LIGHT};
                color: {Theme.SUCCESS};
                border-left-color: {Theme.SUCCESS};
            }}
            
            QLabel#message[type="warning"] {{
                background-color: {Theme.WARNING_LIGHT};
                border-color: {Theme.WARNING_LIGHT};
                color: {Theme.WARNING};
                border-left-color: {Theme.WARNING};
            }}
            
            QLabel#message[type="error"] {{
                background-color: {Theme.DANGER_LIGHT};
                border-color: {Theme.DANGER_LIGHT};
                color: {Theme.DANGER};
                border-left-color: {Theme.DANGER};
            }}
            
            QLabel#message[type="info"] {{
                background-color: {Theme.INFO_LIGHT};
                border-color: {Theme.INFO_LIGHT};
                color: {Theme.INFO};
                border-left-color: {Theme.INFO};
            }}
            
            /* 对话框样式 */
            QDialog {{
                background-color: {Theme.BG_COLOR};
                border-radius: {Theme.BORDER_RADIUS_BASE}px;
                border: 1px solid {Theme.BORDER_LIGHT};
            }}
            
            QDialog QLabel#title {{
                font-size: {Theme.FONT_SIZE_LARGE}px;
                font-weight: bold;
                color: {Theme.TEXT_PRIMARY};
                background-color: {Theme.BG_COLOR};
                padding: 20px 20px 10px 20px;
                border-top-left-radius: {Theme.BORDER_RADIUS_BASE}px;
                border-top-right-radius: {Theme.BORDER_RADIUS_BASE}px;
            }}
            
            QDialog QLabel#content {{
                font-size: {Theme.FONT_SIZE_BASE}px;
                color: {Theme.TEXT_REGULAR};
                padding: 10px 20px 20px 20px;
                line-height: 1.5;
            }}
            
            QDialog QPushButton#close-button {{
                background-color: transparent;
                border: none;
                color: {Theme.TEXT_SECONDARY};
                font-size: 18px;
                font-weight: bold;
            }}
            
            QDialog QPushButton#close-button:hover {{
                color: {Theme.DANGER};
            }}
            
            QWidget#button_container {{
                background-color: {Theme.BG_COLOR_PAGE};
                border-top: 1px solid {Theme.BORDER_LIGHT};
                border-bottom-left-radius: {Theme.BORDER_RADIUS_BASE}px;
                border-bottom-right-radius: {Theme.BORDER_RADIUS_BASE}px;
                padding: 10px 20px;
            }}
            
            /* 类型特定样式 */
            QDialog[type="info"] QLabel#icon {{
                background-color: {Theme.INFO_LIGHT};
                border-radius: 16px;
                border: 1px solid {Theme.INFO_LIGHT};
            }}
            
            QDialog[type="success"] QLabel#icon {{
                background-color: {Theme.SUCCESS_LIGHT};
                border-radius: 16px;
                border: 1px solid {Theme.SUCCESS_LIGHT};
            }}
            
            QDialog[type="warning"] QLabel#icon {{
                background-color: {Theme.WARNING_LIGHT};
                border-radius: 16px;
                border: 1px solid {Theme.WARNING_LIGHT};
            }}
            
            QDialog[type="error"] QLabel#icon {{
                background-color: {Theme.DANGER_LIGHT};
                border-radius: 16px;
                border: 1px solid {Theme.DANGER_LIGHT};
            }}
            
            /* 抽屉组件样式 */
            QDialog#drawer {{
                background-color: rgba(0, 0, 0, 0.5);
                border: none;
            }}
            
            QFrame#drawer-container {{
                background-color: {Theme.BG_COLOR};
                border: 1px solid {Theme.BORDER_LIGHT};
            }}
            
            QWidget#drawer-title-bar {{
                background-color: {Theme.BG_COLOR};
                border-bottom: 1px solid {Theme.BORDER_LIGHTER};
                padding: 16px 20px;
            }}
            
            QLabel#drawer-title {{
                font-size: {Theme.FONT_SIZE_LARGE}px;
                font-weight: bold;
                color: {Theme.TEXT_PRIMARY};
            }}
            
            QPushButton#drawer-close-button {{
                background-color: transparent;
                border: none;
                color: {Theme.TEXT_SECONDARY};
                font-size: 18px;
                font-weight: bold;
            }}
            
            QPushButton#drawer-close-button:hover {{
                color: {Theme.DANGER};
            }}
            
            QScrollArea#drawer-scroll-area {{
                background-color: {Theme.BG_COLOR};
                border: none;
            }}
            
            QWidget#drawer-content {{
                background-color: {Theme.BG_COLOR};
                padding: 20px;
            }}
        """
    
    def _get_component_style_sheet(self, component_name):
        """获取组件的样式表"""
        style_sheets = {
            "button": f"""
                QPushButton {{
                    background-color: {Theme.PRIMARY};
                    color: white;
                    border: none;
                    border-radius: {Theme.BORDER_RADIUS_BASE}px;
                    padding: 8px 16px;
                    outline: none;
                }}
                QPushButton:hover {{
                    background-color: {Theme.PRIMARY_LIGHT};
                }}
                QPushButton:pressed {{
                    background-color: {Theme.PRIMARY};
                }}
                QPushButton:disabled {{
                    background-color: {Theme.INFO_LIGHT};
                    color: {Theme.TEXT_PLACEHOLDER};
                }}
                
                QPushButton[type="success"] {{
                    background-color: {Theme.SUCCESS};
                }}
                QPushButton[type="success"]:hover {{
                    background-color: {Theme.SUCCESS_LIGHT};
                }}
                
                QPushButton[type="warning"] {{
                    background-color: {Theme.WARNING};
                }}
                QPushButton[type="warning"]:hover {{
                    background-color: {Theme.WARNING_LIGHT};
                }}
                
                QPushButton[type="danger"] {{
                    background-color: {Theme.DANGER};
                }}
                QPushButton[type="danger"]:hover {{
                    background-color: {Theme.DANGER_LIGHT};
                }}
                
                QPushButton[type="info"] {{
                    background-color: {Theme.INFO};
                }}
                QPushButton[type="info"]:hover {{
                    background-color: {Theme.INFO_LIGHT};
                }}
                
                QPushButton[plain="true"] {{
                    background-color: transparent;
                    color: {Theme.PRIMARY};
                    border: 1px solid {Theme.PRIMARY};
                }}
                QPushButton[plain="true"]:hover {{
                    background-color: {Theme.PRIMARY_LIGHTER};
                }}
                
                QPushButton[round="true"] {{
                    border-radius: 20px;
                }}
                
                QPushButton[size="large"] {{
                    font-size: {Theme.FONT_SIZE_LARGE}px;
                    padding: 12px 20px;
                }}
                
                QPushButton[size="small"] {{
                    font-size: {Theme.FONT_SIZE_SMALL}px;
                    padding: 6px 12px;
                }}
                
                QPushButton[size="mini"] {{
                    font-size: {Theme.FONT_SIZE_EXTRA_SMALL}px;
                    padding: 4px 8px;
                }}
            """,
            "table": f"""
                QTableView {{
                    border: 1px solid {Theme.BORDER_LIGHT};
                    border-radius: {Theme.BORDER_RADIUS_BASE}px;
                    background-color: {Theme.BG_COLOR};
                    gridline-color: {Theme.BORDER_LIGHTER};
                    outline: none;
                }}
                
                QTableView::item {{
                    padding: 8px;
                    border-bottom: 1px solid {Theme.BORDER_LIGHTER};
                }}
                
                QTableView::item:selected {{
                    background-color: {Theme.PRIMARY_LIGHTER};
                    color: {Theme.PRIMARY};
                }}
                
                QHeaderView::section {{
                    background-color: {Theme.BG_COLOR_PAGE};
                    padding: 8px;
                    border: none;
                    border-bottom: 1px solid {Theme.BORDER_LIGHT};
                    border-right: 1px solid {Theme.BORDER_LIGHT};
                    font-weight: bold;
                    color: {Theme.TEXT_REGULAR};
                }}
                
                QHeaderView::section:checked {{
                    background-color: {Theme.PRIMARY_LIGHTER};
                    color: {Theme.PRIMARY};
                }}
            """,
            "message": f"""
                QLabel#message {{
                    border-radius: {Theme.BORDER_RADIUS_BASE}px;
                    padding: 10px 16px;
                    background-color: {Theme.BG_COLOR};
                    border: 1px solid {Theme.BORDER_LIGHT};
                    color: {Theme.TEXT_REGULAR};
                    font-size: {Theme.FONT_SIZE_BASE}px;
                    font-weight: bold;
                }}
                
                QLabel#message[type="success"] {{
                    background-color: {Theme.SUCCESS_LIGHT};
                    border-color: {Theme.SUCCESS_LIGHT};
                    color: {Theme.SUCCESS};
                    border-left: 4px solid {Theme.SUCCESS};
                }}
                
                QLabel#message[type="warning"] {{
                    background-color: {Theme.WARNING_LIGHT};
                    border-color: {Theme.WARNING_LIGHT};
                    color: {Theme.WARNING};
                    border-left: 4px solid {Theme.WARNING};
                }}
                
                QLabel#message[type="error"] {{
                    background-color: {Theme.DANGER_LIGHT};
                    border-color: {Theme.DANGER_LIGHT};
                    color: {Theme.DANGER};
                    border-left: 4px solid {Theme.DANGER};
                }}
                
                QLabel#message[type="info"] {{
                    background-color: {Theme.INFO_LIGHT};
                    border-color: {Theme.INFO_LIGHT};
                    color: {Theme.INFO};
                    border-left: 4px solid {Theme.INFO};
                }}
            """,
            "message_box": f"""
                QDialog {{
                    background-color: {Theme.BG_COLOR};
                    border-radius: {Theme.BORDER_RADIUS_BASE}px;
                    border: 1px solid {Theme.BORDER_LIGHT};
                }}
                
                QWidget#title_bar {{
                    background-color: {Theme.BG_COLOR};
                    border-top-left-radius: {Theme.BORDER_RADIUS_BASE}px;
                    border-top-right-radius: {Theme.BORDER_RADIUS_BASE}px;
                    border-bottom: 1px solid {Theme.BORDER_LIGHTER};
                }}
                
                QLabel#title {{
                    font-size: {Theme.FONT_SIZE_LARGE}px;
                    font-weight: bold;
                    color: {Theme.TEXT_PRIMARY};
                }}
                
                QPushButton#close-button {{
                    background-color: transparent;
                    border: none;
                    color: {Theme.TEXT_SECONDARY};
                    font-size: 18px;
                    font-weight: bold;
                }}
                
                QPushButton#close-button:hover {{
                    color: {Theme.DANGER};
                }}
                
                QWidget#content_widget {{
                    background-color: {Theme.BG_COLOR};
                }}
                
                QLabel#icon {{
                    min-width: 32px;
                    min-height: 32px;
                }}
                
                QLabel#content {{
                    font-size: {Theme.FONT_SIZE_BASE}px;
                    color: {Theme.TEXT_REGULAR};
                    line-height: 1.5;
                }}
                
                QWidget#button_container {{
                    background-color: {Theme.BG_COLOR_PAGE};
                    border-top: 1px solid {Theme.BORDER_LIGHT};
                    border-bottom-left-radius: {Theme.BORDER_RADIUS_BASE}px;
                    border-bottom-right-radius: {Theme.BORDER_RADIUS_BASE}px;
                }}
                
                QPushButton#confirm_button {{
                    min-width: 80px;
                }}
                
                QPushButton#cancel_button {{
                    min-width: 80px;
                }}
                
                /* 类型特定样式 */
                QDialog[type="info"] QLabel#icon {{
                    background-color: {Theme.INFO_LIGHT};
                    border-radius: 16px;
                    border: 1px solid {Theme.INFO_LIGHT};
                }}
                
                QDialog[type="success"] QLabel#icon {{
                    background-color: {Theme.SUCCESS_LIGHT};
                    border-radius: 16px;
                    border: 1px solid {Theme.SUCCESS_LIGHT};
                }}
                
                QDialog[type="warning"] QLabel#icon {{
                    background-color: {Theme.WARNING_LIGHT};
                    border-radius: 16px;
                    border: 1px solid {Theme.WARNING_LIGHT};
                }}
                
                QDialog[type="error"] QLabel#icon {{
                    background-color: {Theme.DANGER_LIGHT};
                    border-radius: 16px;
                    border: 1px solid {Theme.DANGER_LIGHT};
                }}
            """
        }
        
        return style_sheets.get(component_name, "") 